package com.rerun.common.redisMQ;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.DefaultMessage;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.UnsupportedEncodingException;

/**
 * redis消息监听.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/18 14:21
 */
public class RedisDelegateMessageListener implements MessageListener {
    private static final Logger log = LoggerFactory.getLogger("redisMQ ");

    private RedisTemplate<String, Object> redisTemplate;
    private MessageHandler messageHandler;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            /*System.out.println("channel:" + new String(message.getChannel())
                    + ",message:" + new String(message.getBody(), "utf-8").substring(7));*/
            DefaultMessage defaultMessage= (DefaultMessage) message;
            System.out.println("channel:" + new String(message.getChannel())
                    + ",message:" + new String(defaultMessage.getBody(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public MessageHandler getMessageHandler() {
        return messageHandler;
    }

    public void setMessageHandler(MessageHandler messageHandler) {
        this.messageHandler = messageHandler;
    }

	public RedisTemplate<String, Object> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
    
}
